package base.thread_JUC2.chapter04.demo01;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 模拟任务
 */
public class Task implements Runnable{
    private Date initDate;
    private String name;
    public Task(String name){
        this.initDate = new Date();
        this.name = name;
    }

    @Override
    public void run() {
        System.out.printf("%s: Task %s Created on %s\n", Thread.currentThread().getName(), name, initDate);
        System.out.printf("%s: Task %s Started on %s\n", Thread.currentThread().getName(), name, new Date());

        // 模拟任务执行耗时
//        Util.sleepX((int)(Math.random()*10));
        try {
            Long duration=(long)(Math.random()*10);
            TimeUnit.SECONDS.sleep(duration);
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.printf("%s: Task %s Finished on %s\n", Thread.currentThread().getName(), name, new Date());
    }
}
